import request from '../utils/request';

// 获取产品列表
export function getProducts(params) {
  return request({
    url: '/api/product/page',
    method: 'get',
    params
  });
}

// 获取产品详情
export function getProductById(id) {
  return request({
    url: `/api/product/${id}`,
    method: 'get'
  });
}

// 创建订单
export function createOrder(data) {
  console.log('API层创建订单请求数据:', data);
  return request({
    url: '/api/order',
    method: 'post',
    data
  }).then(response => {
    console.log('API层创建订单响应:', response);
    return response;
  }).catch(error => {
    console.error('API层创建订单错误:', error);
    throw error;
  });
}

// 获取我的购买订单
export function getMyBuyOrders() {
  return request({
    url: '/api/order/my/buy',
    method: 'get'
  });
}

// 获取订单详情
export function getOrderById(id) {
  return request({
    url: `/api/order/${id}`,
    method: 'get'
  });
}

// 支付订单
export function payOrder(id) {
  return request({
    url: `/api/order/${id}/pay`,
    method: 'put'
  });
}

// 确认收货
export function receiveOrder(id) {
  return request({
    url: `/api/order/${id}/receive`,
    method: 'put'
  });
}

// 取消订单
export function cancelOrder(id) {
  console.log(`调用取消订单API，订单ID: ${id}`);
  return request({
    url: `/api/order/${id}/cancel`,
    method: 'put'
  }).then(response => {
    console.log('取消订单API响应:', response);
    return response;
  }).catch(error => {
    console.error('取消订单API错误:', error);
    throw error;
  });
}

/**
 * 获取所有市场上架的产品
 */
export const fetchAllProducts = () => {
  console.log('API调用: 获取所有市场产品');
  return request({
    url: '/api/product/market',
    method: 'get'
  }).then(response => {
    console.log('API响应: 获取所有市场产品', response);
    return response;
  }).catch(error => {
    console.error('API错误: 获取所有市场产品失败', error);
    throw error;
  });
};

// 获取投诉列表
export function getComplaints() {
  return request({
    url: '/api/complaint/my',
    method: 'get'
  });
}

// 创建投诉
export function createComplaint(data) {
  return request({
    url: '/api/complaint',
    method: 'post',
    data
  });
}

// 获取投诉详情
export function getComplaintDetail(id) {
  return request({
    url: `/api/complaint/${id}`,
    method: 'get'
  });
}

// 跟进投诉
export function followUpComplaint(id, content) {
  return request({
    url: `/api/complaint/${id}/followup`,
    method: 'put',
    data: { content }
  });
}

// 关闭投诉
export function closeComplaint(id) {
  return request({
    url: `/api/complaint/${id}/close`,
    method: 'put'
  });
}

// 获取买家交易记录
export function getTransactions(params) {
  console.log('开始获取交易记录, 参数:', params);
  return request({
    url: '/api/transaction/my',
    method: 'get',
    params
  }).then(response => {
    console.log('获取交易记录成功:', response);
    return response;
  }).catch(error => {
    console.error('API错误: 获取交易记录失败', error);
    throw error;
  });
} 